package someThingelse;

import java.io.FileOutputStream;

/**
 * Created by losor on 2017/3/17.
 *
 *
 * 从1，0组成的二位数组中，寻找能组成的最大矩形的面积。
 */
public class FindBigestRectangle {

    public static void main(String[] args) {
        int[][] a = new int[3][];
        a[0] = new int[]{0,1,1,1,0};
        a[1] = new int[]{0,1,0,1,0};
        a[2] = new int[]{0,1,1,0,0};
        FindBigestRectangle f = new FindBigestRectangle();
        System.out.println(f.findBigestRectangle(a));
    }
    public int findBigestRectangle(int[][] nums) {
        int max = findmax(nums[0]);
        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < nums[0].length; j++) {
                if (nums[i][j] == 1) {
                    nums[i][j] = nums[i - 1][j] + 1;
                }
            }
            int t = findmax(nums[i]);
            max = t > max ? t : max;
        }
        return max;
    }

    public int findmax(int[] nums) {
        int count = 0;
        int min = Integer.MAX_VALUE;
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            if (count == 0) {
                if (nums[i] != 0) {
                    count++;
                    min = nums[i];
                }
            }
            else{
                if (nums[i] != 0) {
                    count++;
                    min = nums[i]>min?min:nums[i];
                }
                else{
                    max = min*count>max?min*count:max;
                    min = Integer.MAX_VALUE;
                    count = 0;
                }
            }
        }
        return max;
    }
}
